/*
 * @Author: 0x9DEFA478
 * @Date: 2022-02-13 18:21:00
 * @LastEditTime: 2022-03-06 14:07:35
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "./H_NES_Console.h"
#include "../ConstString/H_NES_ConstString.h"





static Huint16 Noise_TimerPeriodTable_NTSC[16]={
  2U,4U,8U,16U,32U,48U,64U,80U,101U,127U,190U,254U,381U,508U,1017U,2034U
};
static Huint16 Noise_TimerPeriodTable_PAL[16]={
  2U,4U,7U,15U,30U,44U,59U,74U,94U,118U,177U,236U,354U,472U,945U,1889U
};
static Huint16 DMC_TimerPeriodTable_NTSC[16]={
  214U,190U,170U,160U,163U,127U,113U,107U,95U,80U,71U,64U,53U,42U,36U,27U
};
static Huint16 DMC_TimerPeriodTable_PAL[16]={
  199U,177U,158U,149U,138U,118U,105U,99U,88U,74U,66U,59U,49U,39U,33U,25U
};




void H_NES_APU_Reset(H_NES_Console* Console){

  /**
   * 
   * LPF k = 1/(1+Freq/(2*pi*Fc))
   * LPF a = (int)(65536/k)
   * Y(n) = Y(n-1)*(1-k) + X(n)*k = Y(n-1)+(X(n)-Y(n-1))*k = Y(n-1)+(X(n)-Y(n-1))*65536/a
   * 
   * HPF k = (Freq/(2*pi*Fc))/(1+(Freq/(2*pi*Fc)))
   * HPF a = (int)(65536/k)
   * Y(n) = Y(n-1)*k + X(n)*k - X(n-1)*k = (Y(n-1)+X(n)-X(n-1))*65536/a
   * 
   */
  
  Console->CPU.APU.Output.LPF.a=398893;
  //Console->CPU.APU.Output.HPF.a=65537;

  switch(Console->TelevisionStandards){
    case vH_NES_TelevisionStandards_NTSC:

      Console->CPU.APU.Channel.Noise.TimerPeriodTable=Noise_TimerPeriodTable_NTSC;
      Console->CPU.APU.Channel.DMC.TimerPeriodTable=DMC_TimerPeriodTable_NTSC;

      break;
    case vH_NES_TelevisionStandards_PAL:

      Console->CPU.APU.Channel.Noise.TimerPeriodTable=Noise_TimerPeriodTable_PAL;
      Console->CPU.APU.Channel.DMC.TimerPeriodTable=DMC_TimerPeriodTable_PAL;

      break;
    default://Dendy

      Console->CPU.APU.Channel.Noise.TimerPeriodTable=Noise_TimerPeriodTable_NTSC;
      Console->CPU.APU.Channel.DMC.TimerPeriodTable=DMC_TimerPeriodTable_NTSC;

      break;
  }



  Console->CPU.APU.FrameCounter.Reg_4017_CPUClock=(0x04U<<24)|0U;

  Console->CPU.APU.Channel.Pulse1.Timer.Period_Cnt=0x0000U;
  Console->CPU.APU.Channel.Pulse1.Reg_4000=0x00U;
  Console->CPU.APU.Channel.Pulse1.Sweep.TargetTimerPeriod=0U;
  Console->CPU.APU.Channel.Pulse1.Sweep.Reg_4001_PCnt=0x0010U;
  Console->CPU.APU.Channel.Pulse1.Envelope.DividerValue=0U;
  Console->CPU.APU.Channel.Pulse1.Envelope.StartFlag=0U;
  Console->CPU.APU.Channel.Pulse1.Envelope.DecayLevel=0x0FU;
  Console->CPU.APU.Channel.Pulse1.Sequencer.Step=0U;
  Console->CPU.APU.Channel.Pulse1.LengthCounter.Period_Cnt=0x0A00U;

  Console->CPU.APU.Channel.Pulse2.Timer.Period_Cnt=0x0000U;
  Console->CPU.APU.Channel.Pulse2.Reg_4004=0x00U;
  Console->CPU.APU.Channel.Pulse2.Sweep.TargetTimerPeriod=0U;
  Console->CPU.APU.Channel.Pulse2.Sweep.Reg_4005_PCnt=0x0010U;
  Console->CPU.APU.Channel.Pulse2.Envelope.DividerValue=0U;
  Console->CPU.APU.Channel.Pulse2.Envelope.StartFlag=0U;
  Console->CPU.APU.Channel.Pulse2.Envelope.DecayLevel=0x0FU;
  Console->CPU.APU.Channel.Pulse2.Sequencer.Step=0U;
  Console->CPU.APU.Channel.Pulse2.LengthCounter.Period_Cnt=0x0A00U;

  Console->CPU.APU.Channel.Triangle.Timer.Period_Cnt=0x0000U;
  Console->CPU.APU.Channel.Triangle.LinerCounter.Reg_4008_Cnt=0x0000U;
  Console->CPU.APU.Channel.Triangle.LengthCounter.Period_Cnt=0x0A00U;
  Console->CPU.APU.Channel.Triangle.Output=0x80U;

  Console->CPU.APU.Channel.Noise.Timer.Period_Cnt=
    Console->CPU.APU.Channel.Noise.TimerPeriodTable[0]
    |(((Huint32)Console->CPU.APU.Channel.Noise.TimerPeriodTable[0])<<16);
  Console->CPU.APU.Channel.Noise.Reg_400C=0x00U;
  Console->CPU.APU.Channel.Noise.ModeFlag=0x00U;
  Console->CPU.APU.Channel.Noise.Envelope.DividerValue=0U;
  Console->CPU.APU.Channel.Noise.Envelope.StartFlag=0U;
  Console->CPU.APU.Channel.Noise.Envelope.DecayLevel=0x0FU;
  Console->CPU.APU.Channel.Noise.ShiftRegister=0x0001U;
  Console->CPU.APU.Channel.Noise.LengthCounter.Period_Cnt=0x0A00U;

  Console->CPU.APU.Channel.DMC.Timer.Period_Cnt=
    Console->CPU.APU.Channel.DMC.TimerPeriodTable[0]
    |(((Huint32)Console->CPU.APU.Channel.DMC.TimerPeriodTable[0])<<16);
  Console->CPU.APU.Channel.DMC.Reg_4010=0x00U;
  Console->CPU.APU.Channel.DMC.Reader.Address=0xC000U;
  Console->CPU.APU.Channel.DMC.Reader.NumOfLastByte=0U;
  Console->CPU.APU.Channel.DMC.Reader.Period_Address=0xC000U;
  Console->CPU.APU.Channel.DMC.Reader.Period_NumOfLastByte=1U;
  Console->CPU.APU.Channel.DMC.Shifter.NumOfLastBit=8U;
  Console->CPU.APU.Channel.DMC.MultFlag=0xFFU;
  Console->CPU.APU.Channel.DMC.OutputLevel=0x00U;

  Console->CPU.APU.Status.Reg_4015_W=0x00U;

  Console->CPU.APU.Output.OutputIndex=0U;
  Console->CPU.APU.Output.LPF.last=0;


}



